package com.czh.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.czh.auth.application.convert.AuthUserConverter;
import com.czh.auth.api.entity.AuthUserDTO;
import com.czh.auth.api.entity.ResponseResult;
import com.czh.auth.domain.entity.AuthUserBO;
import com.czh.auth.domain.service.AuthUserDomainService;
import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/")
@Slf4j
public class UserController {

    @Resource
    private AuthUserDomainService authUserDomainService;

    /**
     * 登录
     *
     * @param validCode 验证码
     * @return com.czh.subject.common.entity.ResponseResult<cn.dev33.satoken.stp.SaTokenInfo>
     * @since 2024/8/28 下午 12:23
     */
    @RequestMapping("doLogin")
    public ResponseResult<SaTokenInfo> doLogin(@RequestParam("validCode") String validCode) {
        try {
            Preconditions.checkNotNull(validCode, "验证码不能为空!");
            SaTokenInfo saTokenInfo = authUserDomainService.doLogin(validCode);
            return ResponseResult.success(saTokenInfo);
        } catch (Exception e) {
            log.error("UserController:doLogin:error{}", e.getMessage(), e);
            return ResponseResult.error("登录失败!");
        }
    }

    @RequestMapping("logOut")
    public ResponseResult<Boolean> logout(@RequestParam("userName") String userName) {
        try {
            Preconditions.checkNotNull(userName, "用户名不能为空!");
            Boolean logout = authUserDomainService.logout(userName);
            return ResponseResult.success(logout);
        } catch (Exception e) {
            log.error("UserController:logout:error{}", e.getMessage(), e);
            return ResponseResult.error("注销失败!");
        }
    }

    /**
     * 测试快捷接口
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("login")
    public String doLogin(@RequestParam("username") String username, @RequestParam("password") String password) {
        StpUtil.login("oSLGi6bEYnRovC7pSaHcO_bInLS0");
        return StpUtil.getTokenValue();
    }

    @RequestMapping("isLogin")
    public SaResult isLogin() {
        return SaResult.ok("是否登录：" + StpUtil.isLogin());
    }

    /**
     * 注册用户
     *
     * @param authUserDto
     * @return com.czh.subject.common.entity.ResponseResult<java.lang.Boolean>
     * @since 2024/8/28 下午 12:23
     */
    @PostMapping("register")
    public ResponseResult<Boolean> register(@RequestBody AuthUserDTO authUserDto) {
        try {
            checkUserInfo(authUserDto);
            return ResponseResult.success(authUserDomainService.register(AuthUserConverter.INSTANCE.convert(authUserDto)));
        } catch (Exception e) {
            log.error("UserController:register:error{}", e.getMessage(), e);
            return ResponseResult.error("注册失败!");
        }
    }

    /**
     * 获取用户信息
     *
     * @param authUserDto
     * @return com.czh.subject.common.entity.ResponseResult<java.lang.Boolean>
     * @since 2024/8/28 下午 12:23
     */
    @PostMapping("getUserInfo")
    public ResponseResult<AuthUserDTO> getUserInfo(@RequestBody AuthUserDTO authUserDto) {
        try {
            Preconditions.checkNotNull(authUserDto.getUserName(), "用户名不能为空!");
            AuthUserBO authUserBO = AuthUserConverter.INSTANCE.convert(authUserDto);
            return ResponseResult.success(AuthUserConverter.INSTANCE.convert(authUserDomainService.getUserInfo(authUserBO)));
        } catch (Exception e) {
            log.error("UserController:getUserInfo:error{}", e.getMessage(), e);
            return ResponseResult.error("获取用户信息失败!!");
        }
    }

    /**
     * 批量获取用户信息
     */
    @PostMapping("getUserInfoList")
    public ResponseResult<List<AuthUserDTO>> getUserInfoList(@RequestBody List<String> userNameList) {
        try {
            Preconditions.checkState(!userNameList.isEmpty(), "用户名列表不能为空");
            List<AuthUserBO> authUserBOS =  authUserDomainService.getUserInfoList(userNameList);
            return ResponseResult.success(AuthUserConverter.INSTANCE.convert(authUserBOS));
        } catch (Exception e) {
            log.error("UserController:getUserInfoList:error{}", e.getMessage(), e);
            return ResponseResult.error("获取用户信息失败!!");
        }
    }

    /**
     * 修改用户
     *
     * @param authUserDto
     * @return com.czh.subject.common.entity.ResponseResult<java.lang.Boolean>
     * @since 2024/8/28 下午 12:23
     */
    @PostMapping("update")
    public ResponseResult<Boolean> update(@RequestBody AuthUserDTO authUserDto) {
        try {
            Preconditions.checkNotNull(authUserDto.getUserName(), "用户名不能为空!");
            return ResponseResult.success(authUserDomainService.updateUserInfo(AuthUserConverter.INSTANCE.convert(authUserDto)));
        } catch (Exception e) {
            log.error("UserController:update:error{}", e.getMessage(), e);
            return ResponseResult.error("修改用户信息失败!");
        }
    }

    @PostMapping("delete")
    public ResponseResult<Boolean> delete(@RequestBody AuthUserDTO authUserDTO) {
        try {
            Preconditions.checkNotNull(authUserDTO.getId(), "用户id不能为空!");
            return ResponseResult.success(authUserDomainService.deleteUser(authUserDTO.getId()));
        } catch (Exception e) {
            log.error("UserController:delete:error{}", e.getMessage(), e);
            return ResponseResult.error(e.getMessage());
        }
    }

    /**
     * 修改用户状态
     *
     * @param authUserDTO
     * @return bool
     * @since 2024/8/30 下午 9:01
     */
    @PostMapping("changeStatus")
    public ResponseResult<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO) {
        try {
            Preconditions.checkNotNull(authUserDTO.getId(), "用户id不能为空!");
            Preconditions.checkNotNull(authUserDTO.getStatus(), "用户状态不能为空!");
            return ResponseResult.success(authUserDomainService.updateUserInfo(AuthUserConverter.INSTANCE.convert(authUserDTO)));
        } catch (Exception e) {
            log.error("UserController:changeStatus:error{}", e.getMessage(), e);
            return ResponseResult.error("修改用户状态失败");
        }
    }


    private void checkUserInfo(@RequestBody AuthUserDTO authUserDTO) {
        Preconditions.checkNotNull(authUserDTO.getUserName(), "用户名不能为空!");
        Preconditions.checkNotNull(authUserDTO.getNickName(), "用户昵称不能为空!");
        Preconditions.checkNotNull(authUserDTO.getEmail(), "用户邮箱不能为空!");
    }
}